﻿// Time.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "Time.h"
#include <time.h>
#include <stdio.h>

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
HWND hWnd;
// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
char* _gettime(); 
void showWndTopMost(HWND);
wchar_t* _A2U(const char* );
char* _U2A(const wchar_t*);
DWORD WINAPI Threadfun(LPVOID lpParameter);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。
    CreateThread(NULL, 0, Threadfun, 0, 0, 0);
    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_TIME, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TIME));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = NULL;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TIME));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_MY);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_TIME));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中，我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

    hWnd = CreateWindowW(szWindowClass, szTitle, WS_CAPTION| WS_BORDER| WS_SYSMENU,
      0,0,300, 700, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }
 
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
HWND hLab;
HFONT hFont;
HWND hLab1;
HFONT hFont1;
HBITMAP	hbm = (HBITMAP)LoadImage(
    NULL,  // 模块实例句柄
    L"1.bmp",  // 位图路径
    IMAGE_BITMAP,   // 图片类型
    NULL,   // 指定图片宽
    NULL,  // 指定图片高
    LR_LOADFROMFILE  // 从路径处加载图片
);

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{  
    switch (message)
    {
    case WM_CREATE:
        //创建逻辑字体
        hFont = CreateFont(-120/*高*/, -60/*宽*/, 0, 0, 400 /*一般这个值设为400*/,
            FALSE/*斜体?*/, FALSE/*下划线?*/, FALSE/*删除线?*/, DEFAULT_CHARSET,
            OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
            FF_DONTCARE, TEXT("楷体")
        );
        //创建静态文本框控件
        hLab = CreateWindow(TEXT("static"), _A2U(_gettime()),
            WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE /*垂直居中*/,
           0,0, 300, 200,
            hWnd, NULL, hInst, NULL
        );
        SendMessage(hLab, WM_SETFONT, (WPARAM)hFont, NULL);
        //创建逻辑字体
        hFont1 = CreateFont(-28/*高*/, -14/*宽*/, 0, 0, 400 /*一般这个值设为400*/,
            FALSE/*斜体?*/, FALSE/*下划线?*/, FALSE/*删除线?*/, DEFAULT_CHARSET,
            OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
            FF_DONTCARE, TEXT("楷体")
        );
        //创建静态文本框控件
        hLab1 = CreateWindow(TEXT("static"), L"去征服，所有不服！",
            WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE /*垂直居中*/,
            0, 200, 300, 26,
            hWnd, NULL, hInst, NULL
        );
        SendMessage(hLab1, WM_SETFONT, (WPARAM)hFont1, NULL);
        break;
    case WM_NCLBUTTONDOWN://禁止改变大小
        if (wParam == HTCAPTION || (wParam >= HTLEFT && wParam < HTBORDER))
            break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {   
        showWndTopMost(hWnd);
        RECT rctA; //定义一个RECT结构体，存储窗口的长宽高
        GetWindowRect(hWnd, &rctA);//通过窗口句柄获得窗口的大小存储在rctA结构中
        int width = rctA.right - rctA.left;   //窗口的宽度
        int height = rctA.bottom - rctA.top;  //窗口的高度      
        PAINTSTRUCT	ps;
        HDC dc = BeginPaint(hWnd, &ps);
        HDC	memdc = CreateCompatibleDC(dc);
        SelectObject(memdc, hbm);
        SetStretchBltMode(dc, HALFTONE);
        StretchBlt(dc, 0, 220, width, height-220, memdc, 0, 0, 1920, 1080, SRCCOPY);
        DeleteDC(memdc);
        EndPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        }
        break;
    case WM_CLOSE:
       // break;
    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

/*--功能：获取时间*/
char* _gettime()
{
    char* retime = (char*)malloc(30 * sizeof(char));
    time_t nowtime;
    //创建一个time_t 类型的变量char
    struct tm* p;
    //创建一个新时间结构体指针 p
    time(&nowtime);
    //使用该函数就可得到当前系统时间，使用该函数需要将传入time_t类型变量nowtime的地址值。
    p = localtime(&nowtime);
    //此时变量nowtime中的系统时间值为日历时间，调用p=localtime（time_t* nowtime）将nowtime变量中的日历时间转化为本地时间
    sprintf(retime, "%d:%d\0",p->tm_hour, p->tm_min);
    return retime;
}
//功能：窗口前置
void showWndTopMost(HWND hwnd) {
    RECT rect;
    GetWindowRect(hwnd, &rect);
    SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, abs(rect.right - rect.left), abs(rect.bottom - rect.top), SWP_SHOWWINDOW);
}
DWORD WINAPI Threadfun(LPVOID lpParameter) {
    while (1) {
        SetWindowTextW(hLab, _A2U(_gettime()));
        Sleep(60*1000);
    }
    return 0;
}
//将单字节char*转化为宽字节wchar_t*  
wchar_t* _A2U(const char* szStr)
{
    int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0);
    if (nLen == 0)
    {
        return NULL;
    }
    wchar_t* pResult = new wchar_t[nLen];
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen);
    return pResult;
}
//将宽字节wchar_t*转化为单字节char*  
char* _U2A(const wchar_t* szStr)
{
    int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);
    if (nLen == 0)
    {
        return NULL;
    }
    char* pResult = new char[nLen];
    WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);
    return pResult;
}